//
//  WPOrderPayAlertView.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/10.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPOrderPayAlertView.h"

@interface WPOrderPayAlertView ()
@property (nonatomic , strong) UIView * mask;/**< 遮罩视图 */
@property (nonatomic, strong) UILabel * titleLabel;/**< 标题文本 */
@property (nonatomic, strong) UIButton * cancelBtn;/**< 取消按钮 */
@property (nonatomic, strong) UIView * pwdBackView;/**< 密码输入框背景框 */
@property (nonatomic, strong) UIButton * forgetPwdBtn;/**< 忘记密码按钮 */
@property (nonatomic, copy) void (^complete)(NSString *result);/**< 结果block */
@property (nonatomic, strong) UIBezierPath * path;/**< 贝塞尔曲线 */
@property (nonatomic, strong) CAShapeLayer * shapeLayer;/**< 画笔 */
- (void)initializeAppearance;/**< 一般界面初始化 */
@end
@implementation WPOrderPayAlertView
+ (WPOrderPayAlertView *)showViewWithComplete:(void (^)(NSString *))complete {
    WPOrderPayAlertView * view = [[WPOrderPayAlertView alloc]initWithComplete:complete];
    [view show];
    return view;
}
- (instancetype)initWithComplete:(void (^)(NSString *result))complete {
    self = [super init];
    if (self) {
        _complete = complete;
        [self initializeAppearance];
    }
    return self;
}
#pragma mark - init
- (void)initializeAppearance {
    self.frame = CGRectMake(0, SCREEN_SIZE.height - 360*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.width, 360*DHFlexibleHorizontalMutiplier());
    self.backgroundColor = COLOR_RGB(242, 245, 247, 1);
    [self addSubview:self.cancelBtn];
    [self addSubview:self.titleLabel];
    [self addLineWithStartPoint:CGPointMake(0, 40*DHFlexibleHorizontalMutiplier()) andEndPoint:CGPointMake(SCREEN_SIZE.width, 40*DHFlexibleHorizontalMutiplier())];
    [self addSubview:self.forgetPwdBtn];
    [self configNumBtn];
    [self.layer addSublayer:self.shapeLayer];
}
#pragma mark - responds events
- (void)respondsToNumBtn:(UIButton *)sender {
    
}
#pragma mark - private methods
- (void)addLineWithStartPoint:(CGPoint)start andEndPoint:(CGPoint)end {
    [self.path moveToPoint:start];
    [self.path addLineToPoint:end];
}
- (void)configNumBtn {
    for (int i = 0; i < 12; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.bounds = CGRectMake(0, 0, SCREEN_SIZE.width/3.0, 48*DHFlexibleHorizontalMutiplier());
        btn.center = CGPointMake(SCREEN_SIZE.width/6.0+SCREEN_SIZE.width/3.0*(i%3), CGRectGetHeight(self.bounds)-(168 - i/3*48)*DHFlexibleHorizontalMutiplier());
        btn.titleLabel.font = [UIFont systemFontOfSize:24*DHFlexibleHorizontalMutiplier()];
        if (i == 9) {
            btn.backgroundColor = COLOR_RGB(227, 231, 238, 1);
            [self addSubview:btn];
            continue;
        }
        if (i == 10) {
            [btn setTitle:@"0" forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor whiteColor];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self addSubview:btn];
            continue;
        }
        if (i == 11) {
            btn.backgroundColor = COLOR_RGB(227, 231, 238, 1);
            [btn setImage:IMAGE_NAME(@"payDelete") forState:UIControlStateNormal];
            [self addSubview:btn];
            continue;
        }
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitle:[NSString stringWithFormat:@"%d", i+1] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(respondsToNumBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }
}
#pragma mark 显示\隐藏动画
- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self.mask];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.transform = CGAffineTransformTranslate(self.transform, 0, self.frame.size.height);
    [UIView animateWithDuration:0.5 animations:^(void) {
        self.transform = CGAffineTransformIdentity;
    } completion:^(BOOL isFinished) {
    }];
}

- (void)hide
{
    [UIView animateWithDuration:0.5 animations:^(void) {
        self.transform = CGAffineTransformTranslate(self.transform, 0, self.frame.size.height);
        self.mask.alpha = 0;
    } completion:^(BOOL isFinished) {
        [self.mask removeFromSuperview];
        [self removeFromSuperview];
    }];
}
#pragma mark - getter
- (UIView *)mask {
    if (!_mask) {
        _mask = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
            view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
            [view addGestureRecognizer:tap];
            view;
        });
    }
    return _mask;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(20*DHFlexibleHorizontalMutiplier(), 0, 280*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont boldSystemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textAlignment = NSTextAlignmentCenter;
            lab.text = @"输入密码";
            lab;
        });
    }
    return _titleLabel;
}
- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = ({
            UIButton * btn =[ UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(10*DHFlexibleHorizontalMutiplier(), 9*DHFlexibleHorizontalMutiplier(), 22*DHFlexibleHorizontalMutiplier(), 22*DHFlexibleHorizontalMutiplier());
            [btn setImage:[UIImage imageNamed:@"gb"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _cancelBtn;
}
- (UIBezierPath *)path {
    if (!_path) {
        _path = [UIBezierPath bezierPath];
    }
    return _path;
}
- (CAShapeLayer *)shapeLayer {
    if (!_shapeLayer) {
        _shapeLayer = ({
            CAShapeLayer * layer = [CAShapeLayer layer];
            layer.frame = DHFlexibleFrame(CGRectMake(0, 0, 320, 800), NO);
            layer.fillColor = [UIColor whiteColor].CGColor;
            layer.backgroundColor = [UIColor clearColor].CGColor;
            layer.strokeColor = COLOR_RGB(210, 211, 212, 1).CGColor;/**< 画笔颜色 */
            layer.path = self.path.CGPath;
            layer.lineWidth = 0.5;
            layer;
        });
    }
    return _shapeLayer;
}
@end
